import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


public class HtmlFolder 
{

	public static StringBuffer getHtmlDirectory(File path,String url, boolean isRoot)
	{
		StringBuffer out = new StringBuffer();
		out.append("<html>\n");
		//Head
		out.append("<head>\n");
		out.append(String.format("<title>%s</title>",url));
		out.append("<script>\n");
		out.append("function addFileName() {\n document.fileform.action = document.fileform.action+\"\\\\\"+filename(document.fileform.filename.value)+\".txt\";\n }\n");
		out.append("function filename(a) {\n var splitted = a.split(\"\\\\\");\n return splitted[splitted.length-1]; \n }\n");
		out.append("</script>\n");
		out.append("</head>\n");
		
		//Body
		out.append("<body>\n");
		out.append( String.format("<a href=\"%s\">Voltar</a>",getRoot(path)));
		out.append("<table id=\"table\">\n");
		out.append("<tr class=\"header\">\n");
		out.append("<td>Nome</td>\n");
		out.append("<td>Tamanho</td>\n");
		out.append("<td>Type</td>\n");
		out.append("</tr>\n");
		
		String urlHref = url.split("root")[1];
		if(urlHref.length() == 1)
			urlHref = "";
		
		for(File f : path.listFiles())
		{
			out.append("<tr>\n");
			out.append(String.format("<td><a name=\"fileform\" href=%s>%s</a></td>",getLocalPath(f),getLocalName(f)));
			out.append(String.format("<td>%s</td>",getSize(f)));
			out.append(String.format("<td>%s</td>",f.isDirectory()?"Folder":"File"));
			out.append("</tr>\n");
		}
		out.append("</table>\n");
		out.append("<br>\n");
		out.append("<br>\n");
		out.append("<br>\n");
		out.append(String.format("<form name=\"fileform\" action=\"%s\" method=post onSubmit=\"return addFileName()\">",getLocalPath(path)));
		out.append("Nome do arquivo: <input name=\"filename\" size=40>");
		out.append("Texto: <textarea rows=5 cols=34 name=\"text\"></textarea>");
		out.append("<input type=submit value=\"submit\">");
		out.append("</form>");
		out.append("</html>\n");
		return out;
	}
	
	private static String getLocalName(File f)
	{
		String[] path = splitPath(f);
		return path[path.length-1];
	}
	
	private static String getLocalPath(File f)
	{
		String[] pathLocal = splitPath(f);
		String[] pathRoot = splitPath(new File(""));
		String localPath = "";
		for(int i = pathRoot.length; i < pathLocal.length;i++)
		{
			localPath+= "\\"+pathLocal[i];
		}
		
		return localPath;
	}
	
	private static String getRoot(File f)
	{
		String[] pathLocal = splitPath(f);
		String[] pathRoot = splitPath(new File(""));
		String localPath = "";
		for(int i = pathRoot.length; i < pathLocal.length-1;i++)
		{
			localPath+= "\\"+pathLocal[i];
		}
		return localPath+"\\";
	}
	
	private static String[] splitPath(File f)
	{
		String path = f.getAbsolutePath();
		String file = "";
		List<String> pathList= new ArrayList<String>();
		for(int i = 0; i < path.length();i++)
		{
			if(path.charAt(i) == '\\')
			{
				pathList.add(file);
				file = "";
			}
			else
			{
				file +=path.charAt(i);
			}

		}
		pathList.add(file);
		
		String[] dir = new String[pathList.size()];
		for(int i = 0; i < pathList.size();i++)
		{
			dir[i] = pathList.get(i);
		}
		return dir;
	}
	private static String getSize(File f)
	{
		long space = f.length();
		long gb = 1000000;
		gb *= 1000;
		if(space > gb)
			return String.format("%d Gbytes", space/gb);
		if(space > 1000000)
			return String.format("%.2f Mbytes", space/1000000.0);
		if(space > 1000)
			return String.format("%.2f Kbytes", space/1000.0);
		else
			return String.format("%d bytes", space);
	}
	
}